package com.example.administrator.maizuofilm.cinemaschedule;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.example.administrator.maizuofilm.BaseActivity;
import com.example.administrator.maizuofilm.R;
import com.example.administrator.maizuofilm.bean.Filmlist2;
import com.example.administrator.maizuofilm.bean.Films;
import com.example.administrator.maizuofilm.bean.ShowList;
import com.example.administrator.maizuofilm.cinema.view.UrlUtils;
import com.example.administrator.maizuofilm.cinemadetail.CinemaDetailActivity;
import com.example.administrator.maizuofilm.cinemaschedule.adapter.FilmListAdapter;
import com.example.administrator.maizuofilm.cinemaschedule.adapter.GalleyAdapter;
import com.example.administrator.maizuofilm.cinemaschedule.utils.FilmListPaser;
import com.example.administrator.maizuofilm.moviedetails.MovieDetailsActivity;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by a on 2016/7/14.
 */
//public class CinemaSchedulingActivity extends BaseActivity implements CinemaSchedulingContract.CSView {
public class CinemaSchedulingActivity extends BaseActivity {

    @InjectView(R.id.cinema_sche_title)
    TextView cinemaScheTitle;
    @InjectView(R.id.cinema_sche_cname)
    TextView cName;
    @InjectView(R.id.imageView1)
    ImageView imageView1;
    @InjectView(R.id.cinema_sche_go)
    RelativeLayout cinemaScheGo;
    @InjectView(R.id.cinema_sche_gallery)
    Gallery cinemaScheGallery;
    @InjectView(R.id.cinema_sche_filmName)
    TextView cinemaScheFilmName;
    @InjectView(R.id.textView1)
    TextView textView1;
    @InjectView(R.id.cinema_sche_nation)
    TextView cinemaScheNation;
    @InjectView(R.id.textView2)
    TextView textView2;
    @InjectView(R.id.cinema_sche_type)
    TextView cinemaScheType;
    @InjectView(R.id.imageView2)
    ImageView imageView2;
    @InjectView(R.id.cinema_sche_goFilm)
    RelativeLayout cinemaScheGoFilm;
    @InjectView(R.id.cinema_sche_date)
    TextView cinemaScheDate;
    @InjectView(R.id.cinema_sche_tomorrow)
    TextView cinemaScheTomorrow;
    @InjectView(R.id.cinema_sche_after)
    TextView cinemaScheAfter;
    @InjectView(R.id.imageView3)
    ImageView imageView3;
    @InjectView(R.id.textView3)
    TextView textView3;


    //    private CinemaSchedulingContract.CSModel mModel;
//    private CinemaSchedulingContract.CSPresenter mPresenter;
    public static String cinemaName;
    public static String cinemaId;
    @InjectView(R.id.cinema_sche_recyclerview)
    ListView listView;
    @InjectView(R.id.cinema_sche_back)
    ImageView cinemaScheBack;

    private Gallery gallery;

    private List<Films> datas = new ArrayList<Films>();
    private List<String> imageUrl = new ArrayList<String>();
    private List<ShowList> showData = new ArrayList<ShowList>();
    private List<Filmlist2> fList;
    private HttpUtils httpUtils;
    private GalleyAdapter galleyAdapter;
    private FilmListAdapter filmListAdapter;
    //    private MyApplication app;
    private String path;
    private String url;
    private String filmId;
    private String addPath;
    private StringBuffer buffer;
    private String cityId;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cinema_schedule);
        ButterKnife.inject(this);

        //接受上个界面传过来的值
//        Intent intent = getIntent();
//        String filmID = intent.getStringExtra("cinemaName");

//        mModel = new CinemaSchedulingModel();
//        mPresenter = new CinemaSchedulingPresenter(mModel, this);
        httpUtils = new HttpUtils();
//        app = (MyApplication) getApplication();
        cityId = "12";
        initView();
        initInter();
        cinemaScheGo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(CinemaSchedulingActivity.this, CinemaDetailActivity.class));
            }
        });
    }

    @SuppressWarnings("deprecation")
    public void initView() {
//        cName.setText("北京橙天嘉禾影城顺义祥云店");
        cName.setText(cinemaName);
        gallery = (Gallery) findViewById(R.id.cinema_sche_gallery);
    }

    public void initInter() {
        url = UrlUtils.cinemaTime + cityId + UrlUtils.timeMiddle + cinemaId + UrlUtils.timeLast;
//        url = UrlConfig.CINEMA_TIME_FIRST + 12 + UrlConfig.CINEMA_TIME_MIDDLE + 1612 + UrlConfig.CINEMA_TIME_LAST;
//        url = "http://mobileif.maizuo.com/city/12/film1.4/0/cinema/1612/0?channelID=31&clientID=31&agentID=anzhi";
        Log.i("tag", "u+++++r++++l+++" + url);
        httpUtils.send(HttpMethod.GET, url, new RequestCallBack<String>() {

            @Override
            public void onFailure(HttpException arg0, String arg1) {
                throw new RuntimeException("填充数据加载失败");
            }

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String jsonString = responseInfo.result;
                List<ShowList> list = FilmListPaser.getCinemaSche(jsonString);
                showData.addAll(list);
                buffer = new StringBuffer();
                for (ShowList sh : list) {
                    buffer.append(sh.getFilmID() + ",");
                }
                addPath = buffer.toString();
                Log.i("tag", "+++++++++++++++" + addPath);
                initData();
            }
        });
    }

    public void initData() {
//        path = UrlConfig.CINEMA_SCHEDULING_FIRST + 12 + UrlConfig.CINEMA_SCHEDULING_MIDDLE + 2998 + UrlConfig.CINEMA_SCHEDULING_LAST;
//        path = "http://mobileif.maizuo.com/version3/city/12schedulingMiddle=%22/otherFilm?filmIds=2963,2962,3044,3062,2998,2977,3038,2952,3040,3041,3075schedulingLast=%22&clientID=31";
        path = UrlUtils.schedulingPage + cityId + UrlUtils.schedulingMiddle + addPath + UrlUtils.schedulingLast;

        Log.i("tag", "p+++++a++++t+++h" + path);
        httpUtils.send(HttpMethod.GET, path, new RequestCallBack<String>() {

            @Override
            public void onFailure(HttpException arg0, String arg1) {
                throw new RuntimeException("排期数据加载失败");
            }

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String jsonString = responseInfo.result;
                try {
                    List<Films> list = JSON.parseArray(new JSONObject(jsonString)
                            .getString("filmList").toString(), Films.class);
                    datas.addAll(list);
                    for (Films f : list) {
                        imageUrl.add(f.getMinposterAddress());
                    }
                    galleyAdapter = new GalleyAdapter(CinemaSchedulingActivity.this, imageUrl);
                    gallery.setAdapter(galleyAdapter);

                    fList = showData.get(0).getFilmlist2();
                    filmListAdapter = new FilmListAdapter(CinemaSchedulingActivity.this, fList);
                    listView.setAdapter(filmListAdapter);

                    galleryEvent();

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

    }

    public void galleryEvent() {

        gallery.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int position,
                                       long arg3) {
                Films fm = datas.get(position);
                filmId = fm.getFilmID();
                cinemaScheFilmName.setText(fm.getFilmName());
                cinemaScheNation.setText(fm.getNation());
                cinemaScheType.setText(fm.getCategory());

                fList = showData.get(position ).getFilmlist2();
                filmListAdapter = new FilmListAdapter(CinemaSchedulingActivity.this, fList);
                listView.setAdapter(filmListAdapter);

                listView.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> arg0, View arg1,
                                            int position, long arg3) {

                    }
                });
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });
    }


    @OnClick(R.id.cinema_sche_back)
    public void onClick() {
        this.finish();
    }

    @OnClick(R.id.cinema_sche_goFilm)
    public void goFilm(View view) {
        Intent intent = new Intent(CinemaSchedulingActivity.this, MovieDetailsActivity.class);
//        intent.putExtra("filmID", showData.get());
        intent.putExtra("filmID",filmId);
        startActivity(intent);
        Toast.makeText(CinemaSchedulingActivity.this, "转到电影详情", Toast.LENGTH_SHORT).show();
    }


}
